var ns;
var app = new Vue({
    el: '#app',
    data: {
        form: {
            ID: 0,
            Code: '',
            TypeTitles: '',
            TypeIDs: '',
            BranchCode: '',
            BranchCodes: '',
            DeptNames: '',
            StartTime: '',
            StartTimeDesc: '',
            EndTime: '',
            EndTimeDesc: '',
            Content: '',
            keywords: ''
        },
        list: [],
        source: 'pandiantaskdepartmentedit',
    },
    methods: {
        get_data: function() {
            var that = this;
            ns.post({
                action: 'APP_',
                id: that.form.ID
            }, function(succeed, data, err) {
                if (succeed) {
                    that.form = data.data;
                    if (that.form.ID > 0) {
                        that.get_department_data();
                    }
                } else if (err) {
                    ns.toast(err);
                }
            }, {
                toast: true
            });
        },
        get_department_data: function() {
            var that = this;
            var options = {
                action: 'APP_GETALLGWBYBRANCHCODE',
                branchcode: that.form.branchcode,
                keywords: that.form.keywords,
                TaskID: that.form.ID
            };
            ns.post(options, function(succeed, data, err) {
                if (succeed) {
                    that.list = data.list;
                } else if (err) {
                    that.list = [];
                    ns.toast(err);
                }
            }, {
                toast: true
            });
        },
        do_start_pandian: function(item) {
            var that = this;
            var name = 'pandianstart_win';
            var url = '../html/pandianstart_win.html';
            ns.openDirectWin(name, url, {
                TaskID: that.form.ID,
                DepartmentID: item.id,
                LocationID: 0,
                cansavepandian: true,
                canedit: true,
                TaskStatus: that.form.Status
            });
        },
        covertstatus: function(status) {
            if (status == 0)
                return "已完成";
            else if (status == 10)
                return "盘点中";
        },
        search_init: function() {
            var that = this;
            searchBar = document.querySelector(".aui-searchbar");
            searchBarInput = document.querySelector(".aui-searchbar input");
            searchBarBtn = document.querySelector(".aui-searchbar .aui-searchbar-btn");
            searchBarClearBtn = document.querySelector(".aui-searchbar .aui-searchbar-clear-btn");
            if (searchBar) {
                searchBarInput.onclick = function() {
                    searchBarBtn.style.marginRight = 0;
                    searchBarBtn.classList.add("aui-text-info");
                    searchBarBtn.textContent = "搜索";
                }
                searchBarInput.oninput = function() {
                    if (this.value.length) {
                        searchBarBtn.style.marginRight = 0;
                        searchBarClearBtn.style.display = 'block';
                        searchBarBtn.classList.add("aui-text-info");
                        searchBarBtn.textContent = "搜索";
                    } else {
                        searchBarClearBtn.style.display = 'none';
                        searchBarBtn.classList.remove("aui-text-info");
                        searchBarBtn.textContent = "";
                    }
                }
            }
            searchBarClearBtn.onclick = function() {
                searchBarBtn.style.marginRight = "-" + searchBarBtn.offsetWidth + "px";
                this.style.display = 'none';
                searchBarInput.value = '';
                searchBarBtn.classList.remove("aui-text-info");
                searchBarBtn.textContent = "";
            }
            searchBarBtn.onclick = function() {
                var keywords = searchBarInput.value;
                app.form.keywords = keywords;
                if (keywords.length) {
                    searchBarInput.blur();
                } else {
                    this.style.marginRight = "-" + this.offsetWidth + "px";
                    searchBarInput.value = '';
                    searchBarInput.blur();
                    searchBarBtn.textContent = "";
                }
                app.get_department_data();
            }
        }
    }
});
app.search_init();
apiready = function() {
    api.parseTapmode();
    ns = window.Foresight.Util;
    app.form.ID = ns.getPageParam('id') || 0;
    app.get_data();
}
